package com.freesky.quartz.task;

import cn.hutool.core.date.DateUtil;
import com.freesky.biz.abchina.domain.LifepayOrderEntity;
import com.freesky.biz.abchina.pojo.RefundTraceResponse;
import com.freesky.biz.abchina.service.AbChinaLifePayService;
import com.freesky.biz.abchina.service.ILifepayOrderService;
import com.freesky.biz.external.service.MobileCmccService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.config.Constant;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.utils.StringUtils;
import com.freesky.framework.web.service.SendAlarmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 福建农行生活缴费话费充值模块
 */
@Component("lifePayOrderTask")
public class AbChinaLifePayOrderTask {
    private static final Logger logger = LoggerFactory.getLogger(AbChinaLifePayOrderTask.class);

    @Autowired
    private ILifepayOrderService lifepayOrderService;

    @Autowired
    private AbChinaLifePayService lifePayService;

    @Autowired
    private MobileCmccService mobileCmccService;

    @Autowired
    private SendAlarmService sendAlarmService;

    public void taskProcess() {
        logger.info("lifePayOrderTask task start");
        try{
            rechargeProcess();
            refundProcess();
        } catch (Exception e){
            sendAlarmService.sendAlarmMail(Constant.ALARM_MAIL,"【异常】平台-生活缴费话费充值",e.getMessage());
        }
    }

    public void rechargeProcess() {
        List<LifepayOrderEntity> lifepayOrderList = lifepayOrderService.selectUnRechargeList();
        for(LifepayOrderEntity lifepayOrderEntity : lifepayOrderList) {
            lifepayOrderEntity.setProcessed("1");
            lifepayOrderEntity.setProcessedTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            lifepayOrderService.updateById(lifepayOrderEntity);

            String lastReqId = SeqUtil.getSeqId();
            AjaxResult result = mobileCmccService.sendMobileRecharge(
                    lastReqId,
                    String.valueOf(Math.round(Double.valueOf(lifepayOrderEntity.getPayBillAmt())*1000)),
                    lifepayOrderEntity.getMobile());
            String msg = result.getMsg();
            lifepayOrderEntity.setRechargeMsg(msg);
            if(result.isSuccess()) {
                lifepayOrderEntity.setProcessed("2");
                lifepayOrderEntity.setProcessedTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                lifepayOrderEntity.setLastReqId(lastReqId);
                lifepayOrderService.updateById(lifepayOrderEntity);
            } else {
//                if(StringUtils.isNotEmpty(msg)) {
//                    if(msg.indexOf("被付费号码信息不存在")>=0){
//                        lifepayOrderEntity.setNeedRefund("1");
//                    } else if(msg.indexOf("话费统付充值受理:该号码无法充值")>=0){
//                        lifepayOrderEntity.setNeedRefund("1");
//                    } else if(msg.indexOf("数据库操作异常")>=0){
//                        lifepayOrderEntity.setNeedRefund("1");
//                    }
//                }

                lifepayOrderEntity.setNeedRefund("1");
                lifepayOrderEntity.setProcessed("3");
                lifepayOrderEntity.setProcessedTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                lifepayOrderEntity.setLastReqId(lastReqId);
                lifepayOrderService.updateById(lifepayOrderEntity);
            }
        }
    }

    public void refundProcess() {
        List<LifepayOrderEntity> lifepayOrderList = lifepayOrderService.selectRefundOrderList();
        for(LifepayOrderEntity lifepayOrderEntity : lifepayOrderList) {
            AjaxResult refundResult = lifePayService.refundTrace(lifepayOrderEntity.getPayTraceNo(),lifepayOrderEntity.getMerchantId(),lifepayOrderEntity.getPayBillAmt());
            if(!refundResult.isSuccess()) {
                lifepayOrderEntity.setRefundMsg(refundResult.getMsg());
                lifepayOrderEntity.setRefundFlag("2");
                lifepayOrderEntity.setUpdateTime(new Date());
                lifepayOrderService.updateById(lifepayOrderEntity);
                continue;
            }

            RefundTraceResponse refundTraceResponse = (RefundTraceResponse) refundResult.get(AjaxResult.DATA_TAG);
            if(refundTraceResponse!=null) {
                lifepayOrderEntity.setRefundMsg(refundTraceResponse.getMessage().getHead().getReturnMessage());
                if (refundTraceResponse.getMessage().getHead().getReturnCode().equals("0000")) {
                    lifepayOrderEntity.setRefundFlag("1");
                } else {
                    lifepayOrderEntity.setRefundFlag("2");
                }
            } else {
                lifepayOrderEntity.setRefundFlag("2");
                lifepayOrderEntity.setRefundMsg("返回消息为空!");
            }
            lifepayOrderEntity.setUpdateTime(new Date());
            lifepayOrderService.updateById(lifepayOrderEntity);
        }
    }
}
